//商品列表的每个小条目组件
<template>
  <div class="item-good" @click="goToDetails">
    <!-- 绑定一个图片加载完成的事件 -->
    <img :src="getImgUrl" @load="itemImgLoadCompleted" />
    <div>
      <p>{{item.title}}</p>
      <div>
        <span :style="{color:'red','font-size':'14px','font-weight':'bold'}">￥</span>
        <span class="price">{{item.price}}</span>
        <span class="collect right">
          <span class="pic left"></span>
          {{item.cfav}}
        </span>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {};
  },
  methods: {
    //条目的图片加载完成的回调方法
    itemImgLoadCompleted() {
      //图片加载完成时通过VueBus给Home.vue界面发送事件,让Home.vue界面的Better-Scroll对象执行refresh()方法,重新计算滚动高度
      this.$bus.emit("imgLoadCom");
      //同时也向ProductDetails页面中发射一个图片加载完成的事件
      this.$bus.emit('imgHasLoadCom')
    },
    //跳转到商品详情
    goToDetails() {
      this.$router.push({
        path: "/productDetails",
        query: { iid: this.item.iid }
      });
    }
  },
  components: {},
  computed: {
    getImgUrl() {
      return this.item.image || this.item.show.img;
    }
  },
  props: {
    item: {
      type: Object,
      default: {}
    }
  }
};
</script>
<style lang="scss" scoped>
.item-good {
  margin-bottom: 8px;
  width: 48%;
  color: var(--color-text);
  img {
    width: 100%;
    border-radius: 5px;
  }
  p {
    font-size: 12px;
    overflow: hidden;
    text-overflow: ellipsis;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    display: -webkit-box;
    margin-bottom: 4px;
  }
  .price,
  .collect {
    font-size: 12px;
  }
  //定义收藏的图标
  .collect {
    cursor: pointer;
    display: inline-block;
    height: 20px;
    line-height: 20px;
    .pic {
      margin-top: 0.5px;
      margin-right: 2px;
      width: 16px;
      height: 16px;
      background: url("../../../assets/img/home/collect_select.svg") no-repeat;
      background-size: cover;
    }
  }
}
</style>